|
|
|
The function
sqrt finds the square root of a double.
|
|
The cast is made
by putting the bracketed name of the required type just before the value.
(double) in this example.
|
|
When to Cast
|
|
Casting a
variable is a complicated name for a simple concept. When you cast a variable
from one type to another, all you are doing is telling the computer to use a
different type to store the variable. Why would you need (or want) to do
this? Let's say you declared a variable of type short. In most cases, that would mean that the largest positive value you
could store would be 32,767. But somewhere in your program, you realize that
you're going to have to do a calculation which could increase the value over
this maximum. Perhaps you are computing very large Pythagorean triplets. To
calculate the value of c (the hypotenuse), you need to take the square
root of the quantity a2 + b2. But what if a or b
is very large? Then squaring that number will make it much, much larger --
and if the value becomes bigger than 32,767 your values will not be what you
expected (if you had used a short to store a or b. Remember, a short can only store the values between -32,768 and +32,767, so if you try
to store a number out of this range, your data will be incorrect!
|
|
So, the solution
is to cast. We can cast the numbers to a larger data type, such as an int
or a long, for the purposes of the calculation -- and
then we can cast them back to a short when we are done,
since the final value for c will probably be small enough to be stored
in a short.
|
|
This is a
somewhat trivial example, since in this case you could store the numbers in ints
or longs from the beginning and not worry about it! A
more useful example might be if you have a number which represents an
average. You'll probably want to represent the number with a floating-point
type like a float or a double so that it is
accurate while you are computing it (otherwise you'd only be able to store a
value like "26" instead of "26.3141885"). Let's say that
you want to display the value in a table, yet the table would look cluttered
if you displayed "26.3141885", so you decide to simply display the
integer portion, 26. You can cast the float to an int and then display the int
in the table -- since ints can't store floating-point numbers, the
decimal portion of "26.3141885" will be truncated and you will be
left with "26".
|
|
How to Cast
|
|
Casting in C++
is easy. Let's say that you have a float storing a number like
"26.3141885", and you want to have an int
storing the integer portion instead. Here's how to do it:
|
|
int
GetAverage() { // assume that regularAverage and specialAverage store two
floats float totalAverage = regularAverage + specialAverage; // cast
totalAverage to an int int truncatedAverage = (int) totalAverage; // return
the truncated value return truncatedAverage; } There's a little bit of syntax that you haven't
seen before, but the key part to notice is the line of code that reads int truncatedAverage = (int) totalAverage. What we're doing here is taking a float, totalAverage, which stores some kind of decimal number
(like 82.41832), and getting rid of the ".41832" part by casting it
to an int. That works because the int
is only capable of storing integers, so it simply stores the integer portion
of totalAverage.
|